﻿using System;
using UnityEngine;
using UnityEngine.Networking;

[RequireComponent(typeof(Rigidbody))]
public class SimpleBallController : NetworkBehaviour
{
    public Rigidbody body;

    public float throld = 100;

    /// <summary>
    /// 存在服务器上的一个状态
    /// 修改这个状态需要通过Command
    /// </summary>
    private bool alreadyOperatored = false;


    private void Start()
    {
        body = GetComponent<Rigidbody>();

        if (isLocalPlayer)
        {
            FingerGestures.Instance.GetComponent<DragRecognizer>().OnGesture += SimpleBallController_OnGesture;
        }
    }

    private bool startDrag;

    private void SimpleBallController_OnGesture(DragGesture gesture)
    {
        switch (gesture.State)
        {
            case GestureRecognitionState.Ready:
                break;
            case GestureRecognitionState.Started:

                if (gesture.StartSelection == gameObject)
                {
                    startDrag = true;
                }
                break;
            case GestureRecognitionState.InProgress:
                break;
            case GestureRecognitionState.Failed:
            case GestureRecognitionState.Ended:
                if (startDrag)
                {
                    startDrag = false;

                    Cmd_AddForce(gesture.TotalMove, throld);

                    Cmd_ModifyOperator();
                }
                break;
            default:
                break;
        }
    }

    [Command]
    private void Cmd_ModifyOperator()
    {
        alreadyOperatored = true;
    }

    [Command]
    private void Cmd_AddForce(Vector2 totalMove, float throld)
    {
        body.AddForce(new Vector3(totalMove.x, 0, totalMove.y) * throld);
    }

    // Update is called once per frame
    private void Update()
    {
        if (isServer)
        {
            ServerCheckRigidBody();
        }
        //只能控制自己
        if (!isLocalPlayer)
        {
            return;
        }
    }

    #region TurnBaseSystem

    /// <summary>
    /// 客户端状态  需要让每个客户端知道 当前是否应该自己操作
    /// </summary>
    private bool turnMe;
    /// <summary>
    /// 
    /// </summary>
    [ClientRpc]
    public void RpcTurnBegin()
    {
        turnMe = true;

        Debug.Log("你可以操作了");
    }

    /// <summary>
    /// 
    /// </summary>
    [ClientRpc]
    public void RpcTurnEnd()
    {
        turnMe = false;


    }


    [ClientRpc]
    public void RpcAddTurn()
    {
        turnMe = true;
    }

    #endregion

    #region ServerProperties


    #endregion

    private void ServerCheckRigidBody()
    {
        if (alreadyOperatored && body.IsSleeping())
        {
            RpcTurnEnd();

            alreadyOperatored = false;

            TurnBaseSystem.singleton.AddTurnIndex();
        }
    }

    public const string PlayerTag = "Player";

    public void OnCollisionEnter(Collision collision)
    {
        //If collision other player,then your turn.
        if (isServer && collision.gameObject.CompareTag(PlayerTag))
        {
            RpcAddTurn();
        }
    }

    public void OnCollisionExit(Collision collision)
    {
    }

    public void OnGUI()
    {
        if (turnMe && isLocalPlayer)
        {
            GUILayout.Label("轮到你了!");
        }
    }
}
